/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 *
 *
 */
package rad.framework.richfaces;

import static org.jboss.seam.ScopeType.CONVERSATION;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.richfaces.model.TreeNode;

/**
 * The class holds the entity tree during a conversation.
 * 
 * @author boudyacho
 */
@Name("tree")
@Scope(CONVERSATION)
@AutoCreate
@BypassInterceptors
public class RootTreeNode extends BaseTreeNode {

	private static final long serialVersionUID = 1L;

	private EntityTreeNode<?> master;

	private EntityTreeNode<?> current;

	private EntityTreeNode<?> temp;
	
	// used to stop infinite tree recursion & find nodes fast
	private Map<Object, EntityTreeNode<?>> entityNodes = new HashMap<Object, EntityTreeNode<?>>();
	
	public RootTreeNode() {
		super(null);
	}

	//---------------------------------------
	
	public EntityTreeNode<?> init(EntityTreeNode<?> node) {
		if (this.getMaster() == null) {
			node.setParent(this);
			this.setMaster(node);
			this.setCurrent(node);
			if (node.getKey() == null) {
				this.setTemp(node);
			} else {
				this.addChild(node.getKey(), node);
			}
			return node;
		} else {
			if (node.equals(getCurrent())) {
				return getCurrent();
			} else {
				EntityTreeNode<?> match = findFor(node);
				if (match == null) {
					throw new RuntimeException("Tree Init Error. Current is: "
							+ getCurrent().getType() + ":"
							+ getCurrent().getKey() + ", Requested is: "
							+ node.getType() + ":" + node.getKey());
				}
				match.clearChildEditors();
				this.setCurrent(match);
				return match;
			}
		}
	}

	//---------------------------------------

	public List<?> getColumns() {
		return Collections.EMPTY_LIST;
	}
	
	public void addChild(Object id, TreeNode child) {
		EntityTreeNode<?> node = (EntityTreeNode<?>) child;
		// stop recursive relationships
		add(node);
		super.addChild(id, node);
	}

	//---------------------------------------
	
	@Factory(value="masterTree")
	public EntityTreeNode<?> getMaster() {
		return master;
	}

	public void setMaster(EntityTreeNode<?> master) {
		this.master = master;
	}

	@Factory(value="currentTree", scope=ScopeType.PAGE)
	public EntityTreeNode<?> getCurrent() {
		if (temp != null) {
			return temp;
		}
		return current;
	}
	
	public void setCurrent(BaseTreeNode current) {
		temp = null;
		this.current = (EntityTreeNode<?>) current;
		current.getChildren(); // load this level
	}

	public void setTemp(EntityTreeNode<?> temp) {
		this.temp = temp;
	}

	public EntityTreeNode<?> getTemp() {
		return temp;
	}

	//---------------------------------------
	
	protected boolean add(EntityTreeNode<?> node) {
		if (entityNodes.containsKey(node.getInstance())) {
			return false;
		} else {
			entityNodes.put(node.getInstance(), node);
			return true;
		}
	}
	
	protected void remove(EntityTreeNode<?> node) {
		entityNodes.remove(node.getInstance());
	}
	
	public EntityTreeNode<?> findFor(EntityTreeNode<?> node) {
		return entityNodes.get(node.getInstance());
	}
	
	//---------------------------------------

	public RootTreeNode getRoot() {
		return this;
	}

	@Override
	public String getType() {
		return "Root";
	}

	@Override
	public String getText() {
		return "Root";
	}

	@Override
	public Object getKey() {
		return 0;
	}

	@Override
	public String getIcon() {
		return null;
	}
	
	public Object getId() {
		return "root";
	}

}
